package com.example.dangerouscargodriver.ui.fragment;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.controller.IControllerEventListener;
import com.example.dangerouscargodriver.controller.UserInfoController;
import com.example.dangerouscargodriver.event.Event;
import com.example.dangerouscargodriver.event.LoadHttpResourceStatus;
import com.example.dangerouscargodriver.event.LoginStatus;
import com.example.dangerouscargodriver.ui.activity.LogInActivity;
import com.google.gson.Gson;

import java.lang.ref.WeakReference;

public abstract class AuthCheckFragment extends HttpRequestFragment{
    private static final String TAG = "AuthCheckFragment";
    private static final int ACTION_LOGIN = 0;

    private static final int HANDLER_LOGIN = 0;
    private static final int HANDLER_LOGOUT = 1;
    private static final int HANDLER_LOAD_USER_INFO_DONE = 2;

    private java.lang.Class mObject = null;
    private MyControllerListener mController = null;
    private HomeHandler mHandler = null;

    protected abstract void onLogIn();

    protected abstract void onLogOut();

    protected abstract void onUserChanged();

    protected void initAuthCheckComponent(java.lang.Class object){
        mObject = object;
        mController =  new MyControllerListener();
        AuthController.Instance().attachControllerListener(mObject, mController);
        UserInfoController.Instance().attachControllerListener(mObject, mController);

        mHandler = new HomeHandler(this);
    }

    public boolean checkAuth(){
        return AuthController.Instance().checkLogin();
    }

    public void gotoLogInActivity(){
        Intent intent = new Intent(getContext(), LogInActivity.class);
        startActivityForResult(intent, ACTION_LOGIN);
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        AuthController.Instance().unAttachControllerListener(mObject, mController);
        UserInfoController.Instance().unAttachControllerListener(mObject, mController);
    }

    private static class HomeHandler extends Handler {
        private String TAG = "HomeHandler";
        private final WeakReference<AuthCheckFragment> mFragment;

        HomeHandler(AuthCheckFragment fragment) {
            mFragment = new WeakReference<AuthCheckFragment>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            AuthCheckFragment fragment = mFragment.get();
            switch (msg.what){
                case HANDLER_LOGIN:
                    fragment.onLogIn();
                    break;
                case HANDLER_LOGOUT:
                    fragment.onLogOut();
                    break;
                case HANDLER_LOAD_USER_INFO_DONE:
                    fragment.onUserChanged();
                    break;
                default:
                    break;
            }
        }
    }

    private class MyControllerListener implements IControllerEventListener{

        @Override
        public void onEvent(int eventId, String json) {
            if(eventId == Event.EVENT_AUTH){
                Gson gson = new Gson();
                LoginStatus status = gson.fromJson(json, LoginStatus.class);
                Log.d(TAG, "onEvent: " + status.toJson());
                Message message = new Message();
                if(status.getWhat() == AuthController.STATUS_LOGIN){
                    message.what = HANDLER_LOGIN;
                }
                else{
                    message.what = HANDLER_LOGOUT;
                }
                mHandler.sendMessage(message);
            }
            else  if(eventId == Event.EVENT_LOAD_HTTP_USER_INFO){
                Gson gson = new Gson();
                LoadHttpResourceStatus status = gson.fromJson(json, LoadHttpResourceStatus.class);
                Log.d(TAG, "onEvent: " + status.toJson());
                if(status.getWhat() == UserInfoController.EVENT_USER_LOAD_DONE){
                    Message msg = new Message();
                    msg.what = HANDLER_LOAD_USER_INFO_DONE;
                    mHandler.sendMessage(msg);
                }
            }
            else{

            }
        }
    }
}
